package com.hownoon.parquet1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.parquet.hadoop.ParquetInputFormat;
import org.apache.parquet.hadoop.api.ReadSupport;

public class ParquetDriver implements Tool {
    Configuration configuration;
    String readSchema = "";


    @Override
    public int run(String[] args) throws Exception {
        System.out.println(args[0]+"<=====================>"+args[1]);
        long startTime = System.currentTimeMillis();

        configuration.set(ReadSupport.PARQUET_READ_SCHEMA,readSchema);

        Job job = Job.getInstance(configuration, "parquet");

        job.setJarByClass(ParquetDriver.class);
        job.setJobName("parquet");

        job.setMapperClass(ParquetMapper.class);
        job.setInputFormatClass(ParquetInputFormat.class);
        ParquetInputFormat.setReadSupportClass(job, MyReadSupport.class);
        //ParquetInputFormat.addInputPath(job, new Path(args[0]));
        ParquetInputFormat.addInputPath(job, new Path("/storage/1/test"));
        job.setMapOutputKeyClass(NullWritable.class);
        job.setMapOutputValueClass(LongWritable.class);
        //job.setMapOutputValueClass(Put.class);




        job.setReducerClass(ParquetReduce.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(LongWritable.class);
        FileOutputFormat.setOutputPath(job, new Path("/result"));
        /*job.setReducerClass(ParquetReduce.class);
        //job.setOutputFormatClass(TextOutputFormat.class);
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(LongWritable.class);
        //FileOutputFormat.setOutputPath(job, new Path(args[1]));
        FileOutputFormat.setOutputPath(job, new Path("/result"));*/
        job.setPartitionerClass(ParquetPartitioner.class);
        job.setNumReduceTasks(10);

        //TableMapReduceUtil.initTableReducerJob("trade",null,job);


        boolean result=job.waitForCompletion(true);
        long endTime = System.currentTimeMillis();
        ParquetCollet parquetCollet=new ParquetCollet("/result");
        System.out.println("任务耗时"+(endTime - startTime) + "毫秒");


        return result?0:1;
    }

    @Override
    public void setConf(Configuration conf) {
        configuration=conf;
    }

    @Override
    public Configuration getConf() {
        return configuration;
    }

}
